package main

import (
	"flag"
	// "github.com/go-redis/redis"
	"fmt"
	"runtime"

	"github.com/spf13/viper"
)

var (
	conf     *Config
	confPath string
)

func init() {
	flag.StringVar(&confPath, "d", "./job/", " set job config file path")
}

type Config struct {
	Base      BaseConf  `mapstructure:"base"`
	CometConf CometConf `mapstructure:"cometConfig"`
	// Bucket BucketConf `mapstructure:"bucket"`
}

// BaseConf 基础的配置信息
type BaseConf struct {
	Pidfile        string `mapstructure:"pidfile"`
	MaxProc        int
	PprofAddr      string `mapstructure:"pprofBind"` //
	RedisAddr      string `mapstructure:"redisAddr"` //
	RedisPw        string `mapstructure:"redisPw"`
	RedisDefaultDB int    `mapstructure:"redisDefaultDB"`
	PushChan       int    `mapstructure:"pushChan"`
	PushChanSize   int    `mapstructure:"pushChanSize"`
	IsDebug        bool
}
type CometConf struct {
	Addr string `mapstructure:"addr"` // rpc comet层的配置
}

func InitConfig() (err error) {
	conf = NewConfig()
	viper.SetConfigName("job")
	viper.SetConfigType("toml")
	viper.AddConfigPath(confPath)
	if err = viper.ReadInConfig(); err != nil {
		return err
	}
	if err = viper.Unmarshal(&conf); err != nil {
		panic(fmt.Errorf("unable to decode into struct：  %s \n", err))
	}
	return nil
}

func NewConfig() *Config {
	return &Config{
		Base: BaseConf{
			Pidfile:        "/tmp/job.pid",
			MaxProc:        runtime.NumCPU(),
			PprofAddr:      "localhost:6922",
			RedisAddr:      "192.168.51.138:6379",
			RedisPw:        "",
			RedisDefaultDB: 0,
			PushChan:       2,
			PushChanSize:   50,
			IsDebug:        true,
		},
		CometConf: CometConf{
			Addr: "tcp@0.0.0.0:6912",
		},
	}
}

// 重新加载配置
// func ReloadConfig() {
//
// }
